package com.cookie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cookie.common.utils.Result;
import com.cookie.dao.*;
import com.cookie.entity.*;
import com.cookie.service.WxApiService;
import com.cookie.utils.ApiRedisUtils;
import com.google.common.base.Joiner;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : cookie ✌️
 * @date : 2018/11/3 9:45
 */
@Service
public class WxApiServiceImpl implements WxApiService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private IssuesMapper issuesMapper;
    @Autowired
    private HistoryPaperMapper historyPaperMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ApiRedisUtils redisUtils;

    @Autowired
    private RedisTemplate<String, List<Question>> redisTemplate;

    @Override
    public String queryPapers(String types) {
        List<Paper> paper = paperMapper.queryPapers(types);
        return JSON.toJSONString(paper);
    }

    @Override
    public Result saveIssues(Issues issues) {
        issuesMapper.insert(issues);
        return Result.ok("系统已收到您的反馈信息");
    }

    @Override
    public Result issuesCount(String userId) {
        Integer count = issuesMapper.selectCount(new EntityWrapper<Issues>().eq("feed_user", userId));
        return Result.ok().put("count", count);
    }

    @Override
    public List<HistoryPaper> hPapers(String userId) {
        return historyPaperMapper.selectList(new EntityWrapper<HistoryPaper>().eq("user_id", userId));
    }

    @Override
    public Result editUserInfo(User user) {
        Integer i = userMapper.updateById(user);
        return i > 0 ? Result.ok() : Result.error();
    }

    @Override
    public Result queryQuestions(Integer paperId, String category, int page) {
        List<Question> questions = redisTemplate.opsForValue().get(redisKey(paperId, category));//getOperations(redisKey(paperId, category))//get();
        if (CollectionUtils.isEmpty(questions)) {
            questions = questionMapper.selectList(new EntityWrapper<Question>().eq("paper_id", paperId).orderBy("sort"));
            //questions = questionMapper.selectPage(getPage(page), new EntityWrapper<Question>().eq("paper_id", paperId).orderBy("sort"));
            redisTemplate.opsForValue().set(redisKey(paperId, category), questions);
            //redisUtils.hset(redisKey(paperId, category), String.valueOf(page), questions);
        }
        return Result.ok().put("data", questions);
    }

    private RowBounds getPage(int page) {
        page = page != 0 ? page : 1;
        int pageStart = page - 1;
        int pageEnd = page;
        return new RowBounds(pageStart, pageEnd);
    }

    private String redisKey(Integer paperId, String category) {
        return Joiner.on("-").join(category, String.valueOf(paperId));
    }
}
